import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

// 学生端页面
const StudentLayout = () => import('@/views/student/StudentLayout.vue')
const StudentHome = () => import('@/views/student/StudentHome.vue')
const ActivityList = () => import('@/views/student/ActivityList.vue')
const ActivityDetail = () => import('@/views/student/ActivityDetail.vue')
const MyRegistrations = () => import('@/views/student/MyRegistrations.vue')
const MyFeedback = () => import('@/views/student/MyFeedback.vue')

// 组织者端页面
const OrganizerLayout = () => import('@/views/organizer/OrganizerLayout.vue')
const OrganizerHome = () => import('@/views/organizer/OrganizerHome.vue')
const ActivityManagement = () => import('@/views/organizer/ActivityManagement.vue')
const CreateActivity = () => import('@/views/organizer/CreateActivity.vue')
const EditActivity = () => import('@/views/organizer/EditActivity.vue')
const RegistrationManagement = () => import('@/views/organizer/RegistrationManagement.vue')
const FeedbackManagement = () => import('@/views/organizer/FeedbackManagement.vue')

// 管理员端页面
const AdminLayout = () => import('@/views/admin/AdminLayout.vue')
const AdminHome = () => import('@/views/admin/AdminHome.vue')
const AdminActivityManagement = () => import('@/views/admin/ActivityManagement.vue')
const StudentManagement = () => import('@/views/admin/StudentManagement.vue')
const OrganizerManagement = () => import('@/views/admin/OrganizerManagement.vue')
const UserManagement = () => import('@/views/admin/UserManagement.vue')

// 认证页面
const Login = () => import('@/views/auth/Login.vue')
const Register = () => import('@/views/auth/Register.vue')
const Profile = () => import('@/views/auth/Profile.vue')

// 公共页面
const Home = () => import('@/views/Home.vue')
const NotFound = () => import('@/views/NotFound.vue')

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    meta: { requiresAuth: true }
  },
  // 学生端路由
  {
    path: '/student',
    component: StudentLayout,
    meta: { requiresAuth: true, role: 'STUDENT' },
    children: [
      {
        path: '',
        name: 'StudentHome',
        component: StudentHome
      },
      {
        path: 'activities',
        name: 'ActivityList',
        component: ActivityList
      },
      {
        path: 'activities/:id',
        name: 'ActivityDetail',
        component: ActivityDetail
      },
      {
        path: 'registrations',
        name: 'MyRegistrations',
        component: MyRegistrations
      },
      {
        path: 'feedback',
        name: 'MyFeedback',
        component: MyFeedback
      }
    ]
  },
  // 组织者端路由
  {
    path: '/organizer',
    component: OrganizerLayout,
    meta: { requiresAuth: true, role: 'ORGANIZER' },
    children: [
      {
        path: '',
        name: 'OrganizerHome',
        component: OrganizerHome
      },
      {
        path: 'activities',
        name: 'ActivityManagement',
        component: ActivityManagement
      },
      {
        path: 'activities/create',
        name: 'CreateActivity',
        component: CreateActivity
      },
      {
        path: 'activities/:id/edit',
        name: 'EditActivity',
        component: EditActivity
      },
      {
        path: 'registrations',
        name: 'RegistrationManagement',
        component: RegistrationManagement
      },
      {
        path: 'feedback',
        name: 'FeedbackManagement',
        component: FeedbackManagement
      }
    ]
  },
  // 管理员端路由
  {
    path: '/admin',
    component: AdminLayout,
    meta: { requiresAuth: true, role: 'ADMIN' },
    children: [
      {
        path: '',
        name: 'AdminHome',
        component: AdminHome
      },
      {
        path: 'activities',
        name: 'AdminActivityManagement',
        component: AdminActivityManagement
      },
      {
        path: 'students',
        name: 'StudentManagement',
        component: StudentManagement
      },
      {
        path: 'organizers',
        name: 'OrganizerManagement',
        component: OrganizerManagement
      },
      {
        path: 'users',
        name: 'UserManagement',
        component: UserManagement
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 检查是否需要认证
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next('/login')
    return
  }
  
  // 检查角色权限
  if (to.meta.role && userStore.userInfo.role !== to.meta.role) {
    // 根据用户角色重定向到对应的首页
    switch (userStore.userInfo.role) {
      case 'STUDENT':
        next('/student')
        break
      case 'ORGANIZER':
        next('/organizer')
        break
      case 'ADMIN':
        next('/admin')
        break
      default:
        next('/login')
    }
    return
  }
  
  next()
})

export default router